/**
 * @author 黄波
 * 创建日期：2011Sep 28, 2011
 * TODO// N/A
 */
package com.platform.course.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionContext;
import com.platform.common.bo.CommonBo;
import com.platform.constant.ResponseCode;
import com.platform.constant.SaveDir;
import com.platform.course.bo.CourseBo;
import com.platform.pojo.CourseDirectory;
import com.platform.pojo.CourseInfo;
import com.platform.pojo.DirProperty;
import com.platform.pojo.UserInfo;

/**
 * 添加课程信息
 */
public class AddCourseAction extends CourseSupport implements ServletContextAware{

	private CourseInfo item;
	private String msg;
	private boolean success;
    private ServletContext context;
	public ServletContext getContext() {
		return context;
	}
	public void setContext(ServletContext context) {
		this.context = context;
	}
	public CourseInfo getItem() {
		return item;
	}
	public void setItem(CourseInfo item) {
		this.item = item;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	@Override
	public synchronized String execute() throws Exception {

		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfo user = (UserInfo) session.get("user");
		if(user == null || item == null) {
			msg = ResponseCode.FAILURE_ADD;
		} else {
			//基本课程目录
			String baseCourseDir = context.getRealPath(SaveDir.BASE_COURSE_DIR);  
			CourseBo bo = CourseBo.getInstance();
			item.setUserId(user.getId());
//			item.setClassId(0);
			if(bo.addCourse(item)) {
				msg = ResponseCode.SUCCESS_ADD;
				//获得刚添加的课程在数据库表中的id
				CourseInfo leastCourse = bo.getLeastCourse();
				if(leastCourse != null) {
					//添加成功后，在指定目录下，创建文件夹
					CommonBo commonBo = CommonBo.getInstance();
					List<DirProperty> defaultDirs = commonBo.getDirs();
					//将所有默认目录导入到course_directory表中，创建与之对应的记录
					for(DirProperty baseDir : defaultDirs) {
						CourseDirectory cd = new CourseDirectory();
						cd.setCourseId(leastCourse.getId());
						cd.setFolderName(baseDir.getName());
						cd.setFolderEtc(baseDir.getEtc());
						cd.setSuperId(baseDir.getSuperId());
						//导入目录
						bo.addCourseDirectory(cd);
					}
				}
				
				/**
				 * 此创建为物理磁盘目录，此处不需要
				 * 更改向course_directory表导入虚拟目录
				 */
				/*
				for(DirProperty dir : defaultDirs) {
					if(dir.getSuperId() == 0) {
						//为0表示一级目录
						//在基本课程目录下，使用课程ID创建每个课程对应的目录
						File file = new File(baseCourseDir + "/" + item.getId() + "/" + dir.getName());
						if(!file.exists()) {
							file.mkdirs();
						}
					} else {
						File file = new File(baseCourseDir + "/" + item.getId() + "/" + commonBo.getDir(dir.getSuperId().toString()).getName()  + "/" + dir.getName());
						if(!file.exists()) {
							file.mkdirs();
						}
					}
				}
				*/
				
			} else {
				msg = ResponseCode.FAILURE_DB_ERROR;
			}
		}
		success = true;
		HashMap map = new HashMap();
		map.put("msg", msg);
		map.put("success", success);
		JSONObject json = JSONObject.fromObject(map);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(json);
		out.flush();
		out.close();
		return null;
	}
	
    public void setServletContext(ServletContext context) {   
        this.context = context;   
    }   
	
}
